package com.xidian.zhiancloud.service.ledgerManager.relatedPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.RelatedPartyDocMapper;
import com.xidian.zhiancloud.dao.RelatedPartyPeopleDocMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.RelatedPartyDoc;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.relatedParty.RelatedPartyPeopleDocQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyPeopleDocVO;
import com.xidian.zhiancloud.model.RelatedPartyPeopleDoc;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:25
 */
@Service
public class RelatedPartyPeopleDocService extends ServiceImpl<RelatedPartyPeopleDocMapper, RelatedPartyPeopleDoc> implements IService<RelatedPartyPeopleDoc> {

    @Autowired
    private RelatedPartyPeopleDocMapper relatedPartyPeopleDocMapper;
    @Autowired
    private RelatedPartyDocMapper relatedPartyDocMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<RelatedPartyPeopleDocVO> getListByQO(RelatedPartyPeopleDocQO relatedPartyPeopleDocQO) {
        Page<RelatedPartyPeopleDocVO> page = new Page<>(relatedPartyPeopleDocQO.getCurrent(), relatedPartyPeopleDocQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        return relatedPartyPeopleDocMapper.selectByQO(page, relatedPartyPeopleDocQO);
    }

    public RelatedPartyPeopleDocVO getVOById(Long id) {
        RelatedPartyPeopleDoc relatedPartyPeopleDoc = relatedPartyPeopleDocMapper.selectById(id);
        if (relatedPartyPeopleDoc == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        RelatedPartyPeopleDocVO vo = new RelatedPartyPeopleDocVO();
        BeanUtils.copyProperties(relatedPartyPeopleDoc, vo);
        RelatedPartyDoc relatedPartyDoc = relatedPartyDocMapper.selectById(vo.getBelongRelatedPartyId());
        vo.setBelongRelatedPartyName(relatedPartyDoc == null ? "该相关方已被删除" : relatedPartyDoc.getName());
        SysUser user = sysUserMapper.selectById(vo.getCreatorId());
        vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
        return vo;
    }
}
